/**
 * 
 */
package com.honour.controller.interfaced;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honour.service.interfaced.EvaluatelnMapper;
import com.system.controller.base.BaseController;
import com.system.util.AppUtil;
import com.system.util.PageData;
import com.system.util.Tools;
import com.system.util.ToolsUtil;

/**
 * 商品评价接口
 * @author zhanglingbing
 * @date 2018年2月6日
 */
@Controller
@RequestMapping(value = "/appevaluatein")
public class EvaluatelnController extends BaseController  {

	@Resource(name = "evaluatelnService")
	private EvaluatelnMapper evaluatelnService;
	
	
	/**
	 * 添加商品评价
	 * @author zhanglingbing
	 * @date 2018年2月6日
	 * @return
	 */
	@RequestMapping(value = "/addeva")
	@ResponseBody
	public Object addEva() {
		logBefore(logger, "添加商品评价");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		logBefore(logger, pd.toString());
		try {
			if (Tools.checkKey("ADDEVA", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("addEva", pd)) { // 检查参数
					
					pd.put("EVALUATE_ID", this.getSnowId());
					
					/*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					String nowDate = format.format(new Date());
					pd.put("EVALUATEDATE", nowDate);*/
					
					pd.put("EVALUATEDATE", ToolsUtil.getStringDate());
					evaluatelnService.save(pd);
					result = "01";
					
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 用户查询评价列表
	 * @author zhanglingbing
	 * @date 2018年2月6日
	 * @return
	 */
	@RequestMapping(value = "/honevalist")
	@ResponseBody
	public Object honEvaList() {
		logBefore(logger, "用户查询评价列表");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		logBefore(logger, pd.toString());
		try {
			if (Tools.checkKey("HONEVALIST", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("honEvaList", pd)) { // 检查参数
					//分为用户查询跟商家查询
					List<PageData> evaList = evaluatelnService.evaList(pd);
					//pd.get("HONOURUSER_ID") != null && pd.get("HONOURUSER_ID") != ""&&
					if (evaList.size() != 0) {
						for (PageData pageData : evaList) {
//							logBefore(logger, pageData.toString());
							
							String evanickname = (String) pageData.get("EVANICKNAME");
							if ("0".equals(evanickname)) {
								String usenickname = (String) pageData.get("USENICKNAME");
//								logBefore(logger, usenickname);
								String replaceName = usenickname.replaceAll(usenickname.substring(1, usenickname.length()-1), "**");
//								logBefore(logger, replaceName);
								
//								pd.put("USENICKNAME", "");
								pageData.put("USENICKNAME", replaceName);
//								logBefore(logger, pageData.toString());
								
							}
						}
						map.put("pd", evaList);
						
						result = "01";
					}else{
						result = "02";
						
					}
					
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	/**
	 * 商家查询评价列表
	 * @author zhanglingbing
	 * @date 2018年2月8日
	 * @return
	 */
	@RequestMapping(value = "/busevalist")
	@ResponseBody
	public Object busEvaList() {
		logBefore(logger, "查询评价列表");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		logBefore(logger, pd.toString());
		try {
			if (Tools.checkKey("BUSEVALIST", pd.getString("FKEY"))) { // 检验请求key值是否合法
				if (AppUtil.checkParam("busEvaList", pd)) { // 检查参数
					//分为用户查询跟商家查询
					List<PageData> evaList = evaluatelnService.evaList(pd);
//					if (pd.get("BUSINESS_ID") != null && pd.get("BUSINESS_ID") != "") {
					if (evaList.size() != 0) {
						for (PageData pageData : evaList) {
							logBefore(logger, pageData.toString());
							
							String evanickname = (String) pageData.get("EVANICKNAME");
							if ("0".equals(evanickname)) {
								pageData.put("USENICKNAME", "匿名");
								
							}else {
								String usenickname = (String) pageData.get("USENICKNAME");
								String replaceName = usenickname.replaceAll(usenickname.substring(1, usenickname.length()-1), "**");
								pageData.put("USENICKNAME", replaceName);
							}
						}
						
						result = "01";
						map.put("pd", evaList);
					}else{
						result = "02";
					}
//					}
//					result = (0 == evaList.size())?"02":"01";
					
					
				} else {
					result = "03";
				}
			} else {
				result = "05";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
}
